/*
     Copyright 2007 Joseph M. Ferner

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

using System.IO;
using OpenServers.Net.FtpServer.State;

namespace OpenServers.Net.FtpServer.Command {
    /// <summary>
    /// Abstract command.
    /// </summary>
    public abstract class BaseCommand {
        /// <summary>
        /// perform the command. This is called after a user is verified to be logged in.
        /// </summary>
        public abstract void PerformCommand(IFtpServer server, FtpState state, string arguments);

        /// <summary>
        /// Gets the file path given a relative path and the state which contains the current directory.
        /// </summary>
        protected string GetFilePath(FtpState state, string path) {
            string results = Path.Combine(state.CurrentDirectory, path);
            results = results.Replace('\\', '/');
            return results;
        }

        /// <summary>
        ///     <para>
        ///         <b>Errors:</b>
        ///         <list type="bullet">
        ///             <item><term>530</term><description>Not logged in.</description></item>
        ///         </list>
        ///     </para>
        /// </summary>
        public virtual void Invoke(IFtpServer server, FtpState state, string arguments) {
            if (state.User == null) {
                state.Listener.Send(state, server.Messages.Get530());
            } else {
                PerformCommand(server, state, arguments);
            }
        }
    }
}
